/**
 * 遍历表单中所有的控件
 * @param list 表单控件集合 XFormDesign中的 formConfig数据
 * @param callback 回调函数，返回true时结束循环，可用于找控件;
 * callback
 *  参数一：返回当前循环到的控件配置，
 *  参数二：当前控件所属类型：main主表，subTable子表,oneTable一对一子表
 */
let loopControl = (list,callback) =>{
    //循环遍历表单里面的控件，子表与一对一子表直接返回不做深层次的循环
    //当callback返回true的时候跳出循环
    let isTrue = false ;
    let loopFn =(list)=>{
        for (let item of list){
            if(isTrue){ return }
            if(item.type === 'grid'){
                item.children.forEach(gridItem=>loopFn(gridItem.children))
            }else if(item.type === 'table'){
                item.trs.forEach(tr=>{
                    tr.tds.forEach(td=>{
                        loopFn(td.children)
                    })
                })
            }else if(['tabs','collapse'].includes(item.type)){
                item.children.forEach(tabs=>loopFn(tabs.list))
            } else if(['subTable','oneTable'].includes(item.type)){
                if(callback(item,item.type)){
                    isTrue = true
                    return
                }
            }else if(['grouping'].includes(item.type)){
                loopFn(item.children)
            } else  {
                if(callback(item,'main')){
                    isTrue = true
                    return;
                }
            }
        }
    }
    loopFn(list)
}
/**
 * 在表单中删除给定key值的控件
 * @param list {Array} 表单数据
 * @param key {String} 需要删除的控件的key
 */
let delControl = (list,key) =>{
    if(Array.isArray(list) && list.length>0){
        for (let [index,ctl] of list.entries()){
            if(ctl.key === key){
                list.splice(index,1);
                return;
            }else if(['grid','oneTable'].includes(ctl.type)){
                ctl.children.forEach(col=>delControl(col.children,key))
            }else if(ctl.type === 'table'){
                ctl.trs.forEach(tr=>{
                    tr.tds.forEach(td=>{
                        delControl(td.children,key)
                    })
                })
            }else if(['tabs','collapse'].includes(ctl.type)){
                ctl.children.forEach(tab=>delControl(tab.list,key))
            }else if(['grouping'].includes(ctl.type)){
                delControl(ctl.children,key)
            }else if(['subTable'].includes(ctl.type)){
                let delCol = (table,key) =>{
                    for (let [idx,col] of table.entries()){
                        if(col.children){
                            delCol(col.children,key)
                        }else if(col.controlConfig && col.controlConfig.key === key){
                            table.splice(idx,1)
                            return ;
                        }else if(col.key){
                            table.splice(idx,1)
                            return;
                        }
                    }
                }
                delCol(ctl.children,key)
            }
        }
    }
}

/**
 * 获取表单中所有控件的key
 * @param list
 * @param key 可选，如果存在key，则返回当前key的控件
 * @returns {*[]}
 */
let getControlKey = (list,key)=>{
    let keys = []
    let getKeys = (config,key)=>{
        if(key && keys.length > 0){
            return;
        }
        if(Array.isArray(config) && config.length>0){
            for (let ctl of config){
                if(ctl.key === key ){
                    keys.push(ctl)
                    return ;
                }else if(!key){
                    keys.push(ctl.key);
                }
                if(['grid','oneTable'].includes(ctl.type)){
                    ctl.children.forEach(col=>getKeys(col.children,key))
                }else if(ctl.type === 'table'){
                    ctl.trs.forEach(tr=>{
                        tr.tds.forEach(td=>{
                            getKeys(td.children,key)
                        })
                    })
                }else if(['tabs','collapse'].includes(ctl.type)){
                    ctl.children.forEach(tab=>getKeys(tab.list,key))
                }else if(['grouping'].includes(ctl.type)){
                    getKeys(ctl.children,key)
                }else if(['subTable'].includes(ctl.type)){
                    let setCol = (table) =>{
                        for (let col of table){
                            if(col.controlConfig){
                                if(ctl.controlConfig.key === key ){
                                    keys.push(col.controlConfig)
                                    return ;
                                }
                                keys.push(col.controlConfig.key);
                            }else if(col.key){
                                if(ctl.key === key ){
                                    keys.push(col)
                                    return ;
                                }
                                keys.push(col.key);
                            }
                            if(col.children){
                                setCol(col.children)
                            }
                        }
                    }
                    setCol(ctl.children)
                }
            }
        }
    }
    getKeys(list,key);
    return keys ;
}
export {delControl,getControlKey}
export default loopControl
